﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Windows.Controls.Primitives;
namespace HA.ModFlow.Controls.MT3DMSWindows
{
    /// <summary>
    /// winMTOutputControlTimes.xaml 的交互逻辑
    /// </summary>
    public partial class winMTOutputControlTimes : Window
    {
        public winMTOutputControlTimes(List<double> timeSteps)
        {
            InitializeComponent();
            this.Times = timeSteps;
            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste2 = new MenuItem();
            //itemPaste2.Header = "粘贴";
            //itemPaste2.InputGestureText = "Ctrl+V";
            //itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            //cm.Items.Add(itemPaste2);          
            this.dgTimes.ContextMenu = cm;
        }

        List<double> _times = new List<double>();

        public List<double> Times
        {
            get { return _times; }
            set { _times = value; }
        }


        private void btnInitValue_Click(object sender, RoutedEventArgs e)
        {
            winMTOutputControlInitializeTimes winInitTime = new winMTOutputControlInitializeTimes();
            winInitTime.Owner = this;
            if (winInitTime.ShowDialog().Value)
            {
                this.Times = winInitTime.Arry;
                ShowData(winInitTime.Arry);
            }
        }

        private void ShowData(List <double >arr)
        {
            double[]  arry = new double[arr .Count];
            BindingOperations.ClearAllBindings(this .dgTimes);
            this.dgTimes.ItemsSource = null;
            for (int i = 0; i < arr.Count; i++)
            {
                arry[i] = arr[i];
            }
            this.dgTimes.ItemsSource = ConvertToArry("Time", arry).DefaultView;        
        }
        private void GetData()
        {
            this.Times.Clear();         
            foreach (DataRowView dr in dgTimes.Items)
            {
                this.Times.Add(Convert.ToDouble( dr[0].ToString()));              
            }  

        }

        /// <summary>
        /// 把一个一维数组转换为DataTable
        /// </summary>
        /// <param name="ColumnName">列名</param>
        /// <param name="Array">一维数组</param>
        /// <returns>返回DataTable</returns>
        /// <remarks> </remarks>
        public static DataTable ConvertToArry(string ColumnName, double [] Array)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(ColumnName, typeof(double));

            for (int i = 0; i < Array.Length; i++)
            {
                DataRow dr = dt.NewRow();
                dr[ColumnName] = Array[i].ToString();
                dt.Rows.Add(dr);
            }

            return dt;
        }
        

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            GetData();          
            this.DialogResult = true;
            this.Close();
        }

     
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        { 
            if (this .Times .Count >0)
            {
                this.Times.Insert(0, this .Times[0]/2);
                ShowData(this.Times);              
            }          
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            int a = this.dgTimes.SelectedIndex;
            if (a >= 0 && this.dgTimes.Items.Count > 0)
            {
                this.Times.RemoveAt(a);
                ShowData(this.Times);     
                if (a >= this.dgTimes.Items.Count)
                {
                    a = this.dgTimes.Items.Count - 1;
                }
                this.dgTimes.SelectedIndex = a;
            }
        }

        private void dgTimes_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.dgTimes.CanUserAddRows = false;           
            ShowData(this.Times);
           
        }       

       

    }
}
